<?php


namespace App\Http\Controllers\product;

use App\Models\ProductClassBranch;
use App\Models\Worker;
use App\Models\ProductClass;


class utils
{
    // 判断产品管理权限
    public static function AuthorityDecide($worker_id) {
        $worker = Worker::where('id', $worker_id)->with('roles.powers')->first();
        if (isset($worker['roles'])) {
            foreach ($worker['roles'] as $role) {
                if (isset($role['powers'])) {
                    foreach ($role['powers'] as $power) {
                        if ($power['id'] == 19) {
                            return 1;
                        }
                    }
                }
            }
        }
        return 0;
    }

    // 结果分页
    public static function Paging($document_list, $page, $size) {
        $start = ($page - 1) * $size;
        $article = array_slice($document_list, $start, $size);
        $datas_list = [];
        $count = count($document_list);
        $datas_list['total'] = $count;
        $datas_list['data'] = $article;
        return $datas_list;
    }

    // 根据部门得到所有能看的产品类型id——list
    public static function get_classify($branch_id, $type=1) {
        $classify_list = ProductClass::whereHas('branches', function ($query) use ($branch_id){
            $query->where('branch_id', $branch_id);
        })->get();
        if ($type == 1) {
            $classifys = [];
            if ($classify_list) {
                foreach ($classify_list as $classify) {
                    $classifys[] = $classify->id;
                }
            }
            return $classifys;
        } else {
            return $classify_list;
        }
    }

    // 处理产品查询结果
    public static function deal_product_result($product_list) {
        $products = [];
        if ($product_list) {
            foreach ($product_list as $product) {
                $product_dic['name'] = $product->name;
                $product_dic['classify_id'] = $product->class_id;
                $product_dic['classify_name'] = $product['product_class']['category_name'];
                switch ($product->time_unit) {
                    case 1:
                        $product_dic['time_unit'] = '日';
                        break;
                    case 2:
                        $product_dic['time_unit'] = '月';
                        break;
                    case 3:
                        $product_dic['time_unit'] = '年';
                        break;
                    default:
                        $product_dic['time_unit'] = '未知';
                }
                $product_dic['rate'] = $product->rate;
                $product_dic['money'] = $product->money;
                $product_dic['detail'] = $product->detail;
                $product_dic['content'] = $product->content;
                $product_dic['ceiling'] = $product->ceiling;
                $product_dic['sale_time'] = $product->sale_time;
                $product_dic['commission'] = $product->commission;
                switch ($product->commission_type) {
                    case 1:
                        $product_dic['commission_type'] = '年化业绩';
                        break;
                    case 2:
                        $product_dic['commission_type'] = '合同金额';
                        break;
                    case 3:
                        $product_dic['commission_type'] = '其他';
                        break;
                    default:
                        $product_dic['commission_type'] = '未知';
                }
                switch ($product->pay_type) {
                    case 2:
                        $product_dic['pay_type'] = '自然月';
                        break;
                    case 4:
                        $product_dic['pay_type'] = '到期付';
                        break;
                    case 5:
                        $product_dic['pay_type'] = '不付款';
                        break;
                    default:
                        $product_dic['pay_type'] = '未知';
                }
                $product_dic['pay_long'] = $product->pay_long;
                switch ($product->pay_time_unit) {
                    case 1:
                        $product_dic['pay_time_unit'] = '日';
                        break;
                    case 2:
                        $product_dic['pay_time_unit'] = '月';
                        break;
                    case 3:
                        $product_dic['pay_time_unit'] = '年';
                        break;
                    default:
                        $product_dic['pay_type'] = '未知';
                }
                $product_dic['long_time'] = $product->long_time;
                $product_dic['results_rate'] = $product->results_rate;
                $product_dic['year_type'] = $product->year_type;
                $product_dic['remark'] = $product->remark;
                $products[] = $product_dic;
            }
        }
        return $products;
    }

    // 根据产品类型id得到相关分类产品id列表
    public static function get_product_class($classify_id) {
        $classify_list = [];
        $classify = ProductClass::find($classify_id);
        if ($classify) {
            $classify_list[] = $classify_id;
            if ($classify->pid != null && $classify->pid != 0) {
                $p_classify = ProductClass::find($classify->pid);
                if ($p_classify) {
                    $classify_list[] = $p_classify->id;
                }
            } else {
                $classifys = ProductClass::where('pid', $classify_id)->get();
                if ($classifys) {
                    foreach ($classifys as $classify) {
                        $classify_list[] = $classify['id'];
                    }
                }
            }
        }
        return $classify_list;
    }

    // 根据部门获得对应产品列表
    public static function get_branch_classify($branch_id) {
        $classify = [];
        $product_class_list = ProductClassBranch::where('branch_id', $branch_id)->get();
        if ($product_class_list) {
            foreach ($product_class_list as $product_class) {
                $classify[] = $product_class['product_class_id'];
            }
        }
        return $classify;
    }

    // 得到某类产品展示部门的list
    public static function get_class_branch_list($classify_id) {
        $class_branchs = ProductClassBranch::where('product_class_id', $classify_id)->get();
        $branch_list = [];
        if ($class_branchs) {
            foreach ($class_branchs as $class_branch) {
                $branch_list[] = $class_branch['branch_id'];
            }
        }
        return $branch_list;
    }

}
